/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tiebreak.BL;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import tiebreak.BE.Member;

/**
 *
 * @author kennet
 */
public class MemberWorker
{

    public Member member;

    public void setMember(Member newMember)
    {
        this.member = newMember;
    }

    public int addMember(String firstName, String lastName, String address, int zipcode,
            int cpr, int phone, String email, int typeID, boolean isElite,
            boolean underSchoolTraining, boolean isCoach) throws EasyException
    {
        try
        {
            

            if (memberExists(cpr))
            {
                throw new EasyException("A member with the given cpr number \""
                        + cpr + "\" already exists in the database");
            }

            setMember(new Member(firstName, lastName, address, zipcode, cpr, phone, email, typeID, isElite, underSchoolTraining, isCoach));
            return MemberManager.addMember(member);
        }
        catch (SQLException ex)
        { 
            throw new EasyException("There was a problem with the Database");
        }
        catch (IOException ex)
        {
           
            throw new EasyException("There was a problem with the configuration file");
        }
    }

    public Member getMemberWithCpr(int cpr) throws EasyException
    {
        try
        {
            if (checkCprLength(cpr))
            {
                throw new EasyException("Cpr number is not 10 characters long");
            }

            return MemberManager.getMemberWithCpr(cpr);
        }
        catch (IOException ex)
        {
            throw new EasyException("There was a problem with the configuration file");
        }
        catch (SQLException ex)
        {
            
            throw new EasyException("There was a problem with the database");
        }
    }

    public Member getMemberWithMemberID(int memberID) throws EasyException
    {
        try
        {
            if (checkCprLength(memberID))
            {
                throw new EasyException("Cpr number is not 10 characters long");
            }

            return MemberManager.getMemberWithMemberID(memberID);
        }
        catch (IOException ex)
        {
            throw new EasyException("There was a problem with the configuration file");
        }
        catch (SQLException ex)
        {
            throw new EasyException("There was a problem with the Database");
        }
    }

    public void startUpdateMember(int memberID, String firstName, String lastName, String address,
            int zipcode, int cpr, int phone, String email, int typeID,
            boolean isElite, boolean underSchoolTraining, boolean isCoach) throws EasyException
    {
        member = new Member(memberID, firstName, lastName, address, zipcode, cpr, phone, email, typeID, isElite, underSchoolTraining, isCoach);
        if (ignoreField(firstName))
        {
            member.setFirstName(firstName);
        }

        if (ignoreField(lastName))
        {
            member.setLastName(lastName);
        }

        if (ignoreField(address))
        {
            member.setAddress(address);
        }

        if (ignoreField(Integer.toString(zipcode)))
        {
            member.setZipcode(zipcode);
        }

        if (ignoreField(Integer.toString(cpr)))
        {
            member.setCpr(cpr);
        }

        if (ignoreField(Integer.toString(phone)))
        {
            member.setPhone(phone);
        }

        if (ignoreField(email))
        {
            member.setEmail(email);
        }

        if (ignoreField(Integer.toString(typeID)))
        {
            member.setTypeID(typeID);
        }

        member.setMemberID(memberID);
        member.setIsElite(isElite);
        member.setUnderSchoolTraining(underSchoolTraining);
        member.setIsCoach(isCoach);

        checkUpdateMember();
    }

    private void updateMember() throws EasyException
    {
        try
        {
            getMemberWithMemberID(member.getMemberID());
            MemberManager.updateMember(member);
        }
        catch (IOException ex)
        {
            throw new EasyException("There was a problem with the configuration file");
        }
        catch (SQLException ex)
        {
            throw new EasyException("There was a problem with the Database");
        }
    }

    private boolean checkMember()
    {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    

    private boolean checkCprLength(int cpr)
    {
        String stringCpr = Integer.toString(cpr);
        return stringCpr.length() != 10;
    }

    private boolean memberExists(int cpr) throws EasyException
    {
        return (getMemberWithCpr(cpr) != null);
    }

    static public boolean ignoreField(String field)
    {
        return !field.equals("");
    }
    
    
    private boolean checkMemberByMemberId(int memberID) throws SQLException, IOException
    {
        if(MemberManager.getMemberWithMemberID(memberID)==null)
            return false;
        return true;
    }

    public void checkUpdateMember() throws EasyException
    {
        if (member == null)
        {
            throw new EasyException("Member has insufficient data");
        }
        updateMember();
    }

    

    public void delete(int memberID) throws EasyException
    {
        try
        {
            checkMemberByMemberId(memberID);
            MemberManager.delete(memberID);
        }
         catch (IOException ex)
        {
            throw new EasyException("There was a problem with the configuration file");
        }
        catch (SQLException ex)
        {
            throw new EasyException("There was a problem with the Database");
        }
    }
}
